package com.example.demo1.util;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.POIDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author zhangzichang
 * @Description: 解析新疆自治区/兵团运输信息工程数据库word文档，并把列注释保存到数据库中
 * @company：CTTIC
 * @since 2021/4/25 17:20
 */
public class WordUtil {
    public static void main(String[] args) throws IOException {
        String fileName = "E:\\company\\project\\新疆运政数据字典.docx";
        FileInputStream fs = new FileInputStream(new File(fileName));

        //开始加载word文档
        XWPFDocument wordDocument = new XWPFDocument(fs);
        System.out.println(wordDocument);
        Iterator<IBodyElement> bodyElementsIterator = wordDocument.getBodyElementsIterator();
        //定义文本和表格初始下标
        AtomicInteger paragraphIndex = new AtomicInteger(0);
        AtomicInteger tableIndex = new AtomicInteger(0);
        //定义所有文本列表和表格列表对象信息
        List<XWPFParagraph> paragraphs = null;
        List<XWPFTable> tables = null;
        while (bodyElementsIterator.hasNext()){
            IBodyElement next = bodyElementsIterator.next();
//            System.out.println(next.getElementType());
            //初始化所有的文本信息和表格信息
            if(CollectionUtils.isEmpty(paragraphs) && CollectionUtils.isEmpty(tables)){
                paragraphs = next.getBody().getParagraphs();
                tables = next.getBody().getTables();
            }

            if(StringUtils.equals(BodyElementType.PARAGRAPH.name(), next.getElementType().name())){
                XWPFParagraph xwpfParagraph = paragraphs.get( paragraphIndex.getAndIncrement() );
                System.out.println(xwpfParagraph.getText());
            }else if(StringUtils.equals(BodyElementType.TABLE.name(), next.getElementType().name())){
                XWPFTable xwpfTable = tables.get(tableIndex.getAndIncrement());
                System.out.println(xwpfTable.getRows().size());
            }

        }
//        List<IBodyElement> bodyElements = wordDocument.getBodyElements();
//        for(IBodyElement bodyElement:bodyElements){
//            BodyElementType elementType = bodyElement.getElementType();
//            System.out.println(elementType.name());
//            if(StringUtils.equals("PARAGRAPH",elementType.name())){
//                XWPFDocument document = (XWPFDocument)bodyElement.getBody();
//                List<XWPFParagraph> paragraphs = document.getParagraphs();
//            }else if(StringUtils.equals("TABLE",elementType.name())){
//                XWPFTable table = (XWPFTable) bodyElement.getBody();
//                System.out.println(table.getText());
//            }

//        }

//        List<XWPFTable> tables = wordDocument.getTables();
//        for(XWPFTable xwpfTable : tables){
//            List<XWPFTableRow> rows = xwpfTable.getRows();
//            XWPFTableRow xwpfTableRow = rows.get(0);
//            List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
//            System.out.println(tableCells.stream().map(XWPFTableCell::getText).collect(Collectors.joining("\t")));
//        }
//        System.out.println(tables.size());
        wordDocument.close();
    }
}
